package com.zqxq.lottery.api.controller;

import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.api.feign.LotteryServerFeignClient;
import com.zqxq.lottery.entity.vo.news.MatchNewsLikesVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * @author 苦智
 * @date 2018/9/5 10:39
 * @description 赛事资讯controller
 * @ClassName MatchNewsController
 */
@Api(value = "赛事资讯接口",tags = "赛事资讯接口")
@RestController
@Slf4j
@RequestMapping(value = "/matchNews",produces = "application/json")
public class MatchNewsController {

    @Autowired
    private LotteryServerFeignClient lotteryServerFeignClient;

    @PostMapping("/home")
    @ApiOperation("资讯列表")
    public Result getHomeNews(@ApiParam(value = "赛事类型0:热门赛事1:足球2:中超3:篮球4:电竞",required = true) @RequestParam(defaultValue = "0") Integer type,
                              @ApiParam(value = "页码",required = true) @RequestParam(defaultValue = "1") Integer pageNo,
                              @ApiParam(value = "每页条数",required = true) @RequestParam(defaultValue = "10") Integer pageSize){
        return lotteryServerFeignClient.getHomeNews(type,pageNo,pageSize);
    }

    @PostMapping("/detail")
    @ApiOperation("资讯详情")
    public Result getDetaiNews(@ApiParam(value = "资讯id",required = true) @RequestParam("newsId") String newsId){
        if (StrUtils.isEmpty(newsId)){
            log.info("资讯id不能为空");
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        }
        return lotteryServerFeignClient.getDetailNews(newsId);
    }

    @PostMapping("/like")
    @ApiOperation("点赞资讯详情（资讯打赏）")
    public Result likeNews(@ApiParam(value = "资讯id",required = true) @RequestParam("newsId") String newsId,
                           @ApiParam(value = "用户id",required = true) @RequestParam("userId") String userId,
                           @ApiParam(value = "打赏金额",required = true) @RequestParam("payAmount") String payAmount){
        if (StrUtils.isEmpty(newsId) || StrUtils.isEmpty(userId) || StrUtils.isEmpty(payAmount) || payAmount.contains("-")){
            log.info("资讯id或用户id不能为空");
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        }

        BigDecimal bigDecimal = new BigDecimal(payAmount);
        if (bigDecimal.compareTo(new BigDecimal(200)) == -1)
            return new Result(CodeEnum.FAIL.getCode(),"打赏金额不能少于200趣豆");
        return lotteryServerFeignClient.getLikeNews(newsId,userId,payAmount);
    }

    @PostMapping("/payReward")
    @ApiOperation("支付打赏")
    public Result payReward(MatchNewsLikesVO vo){
        if (vo == null)
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        if (StrUtils.isEmpty(vo.getLikesId()))
            return new Result(CodeEnum.FAIL_100002.getCode(),"订单id为空");
        return lotteryServerFeignClient.payReward(vo);
    }

    @PostMapping("/click")
    @ApiOperation("保存点击量")
    public Result clickNews(@ApiParam(value = "资讯id",required = true) @RequestParam("newsId") String newsId){
        if (StrUtils.isEmpty(newsId)){
            log.info("资讯id不能为空");
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        }
        return lotteryServerFeignClient.clickNews(newsId);
    }

    @PostMapping("/match")
    @ApiOperation(value = "资讯首页赛事",notes = "资讯首页赛事")
    public Result newsHomeMatch(){
        return lotteryServerFeignClient.newsHomeMatch();
    }
}
